/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.EditPolicy;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagraminterchange2.di2.DiagramElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * This edit part is the parent of edit parts which have several children edit part (like diagrams, class edit parts, etc.). 
 * It is linked to a UML element, as it inherits from {@link GraphNodeWithSemanticEditPart}.
 */
abstract public class ContainerWithSemanticEditPart extends GraphNodeWithSemanticEditPart {

	/**
	 * A container is GraphNode that has contained elements (ex. diagram, package...)
	 * @param modelManager manager in charge of models, graphic and domain
	 * @param graphNode parent of the graph node controlled by this edit part
	 */
	public ContainerWithSemanticEditPart(GraphNode graphNode,ModelManager modelManager) {
		super(graphNode,modelManager);
	}

	/**
	 * Installs the desired EditPolicies for this edit part.
	 */
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		// this is a container so...
		installEditPolicy(EditPolicy.CONTAINER_ROLE,new ContainerWithSemanticEditPolicy());
		installEditPolicy(DimensionAdaptationEditPolicy.DIMENSION_ADAPTATION, new ContainerDimensionAdaptationEditPolicy());
	}

	/**
	 * Returns a <code>List</code> containing the children model objects. 
	 * As this EditPart's model is a container, this method returns its children. This is what causes children EditParts to be
	 * created.
	 * <p>
	 * Callers must not modify the returned List. Must not return <code>null</code>.
	 * </p>
	 * @return the List of children
	 */
	@Override
	protected List getModelChildren() {
		ArrayList contained= new ArrayList();

		// if the presentation of the element is not shape we can display children, otherwise we can't
		if(getGraphNode().getSemanticModel() != null) {
			if((getGraphNode().getSemanticModel().getPresentation()==null) ||
					!(getGraphNode().getSemanticModel().getPresentation().equals(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION))) {
				Iterator<DiagramElement> it=getGraphNode().getContained().iterator();
				while (it.hasNext()){
					DiagramElement ge=(DiagramElement)it.next();
					if (!(ge instanceof GraphEdge)){
						contained.add(ge);
					}
				}
			}
		}
		return contained;
	}

//	/**
//	* @return 
//	*/
//	protected AccessibleEditPart createAccessible() {
//	return new AccessibleGraphicalEditPart() {
//	@Override
//	public void getName(AccessibleEvent e) {
//	e.result = getGraphNode().toString();
//	}
//	};
//	}
}
